
#import "FaceView.h"
#import <c.h>
#import <ctype.h>

@implementation FaceView

//-----------------------------------------------------------------------
// PRIVATE METHODS
//-----------------------------------------------------------------------

// In Ermangelung der passenden RFC eine einfache Heuristik. Vom '@' ausgehend
// wird der Anfang und das Ende des Logins gesucht.
// Fuer eine "richtige" Loesung: Was gibt's fuer Formate?
//   1) login 
//   2) login (real name)
//   3) "real name" <login>
//   4) real name <login>
//   ...

#define islogin(c) (isalnum(c) || (c)=='.' || (c)=='_' || (c)=='%' || (c)=='-')

- (char *)loginForName:(const char *)userName;
	{
	char		login[300],*at,*p;
	int			n;
	
	at=strchr(userName,'@');
	if(at)
		{
		n=1;
		for(p=at+1;islogin(*p);p++)
			n++;
		for(p=at-1;p>=userName && islogin(*p);p--)
			n++;
		strncpy(login,p+1,MIN(n,300));
		login[MIN(n,300)]='\0';
		for(p=login;*p;p++)
			if(*p=='!' || *p=='@')
				*p='.';
			else
				*p=NXToLower(*p);
		at=NXCopyStringBufferFromZone(login,[self zone]);
		}
	return at;
	}


// NewsGrazer Methode. Mail ist cleverer und sieht noch in der Aliases-Datei nach
// und testet auf lokalen Benutzer...

- (char *)filenameForLogin:(const char *)login
	{
	char buf[MAXPATHLEN+1];
	
	sprintf(buf,"/LocalLibrary/Images/People/%s.tiff",login);
	return NXCopyStringBufferFromZone(buf,[self zone]);	
	}


//-----------------------------------------------------------------------
// INSTANCE VAR
//-----------------------------------------------------------------------

- setImage:(NXImage *)anImage;
	{
	myImage=anImage;
	return self;
	}


- (NXImage *)image;
	{
	return myImage;
	}


//-----------------------------------------------------------------------
// SET IMAGE FROM LOGIN NAME
//-----------------------------------------------------------------------
- (BOOL)showFaceForName:(const char *)name;
	{
	NXImage		*image=nil;
	char		*imageFile,*login;

	if(name && (login=[self loginForName:name])!=NULL)
		{
		image=[NXImage findImageNamed:login];
		if(!image)
			{
			imageFile=[self filenameForLogin:login];
			image=[[NXImage allocFromZone:[self zone]] initFromFile:imageFile];
			NXZoneFree([self zone],imageFile);
			if(image && [image lockFocus])
				{
				NXSize size, newsize={64,64};

				[image unlockFocus];
				// [TRH] Don't scale image if it isn't necessary,
				// and keep size/width proportions intact if it is.
				[image getSize:&size];
				if (size.height > newsize.height || size.width > newsize.width)
					{
					if (size.height > size.width)
						newsize.width=newsize.width*size.width/size.height;
					else if (size.height < size.width)
						newsize.height = newsize.height*size.height/size.width;
					[image setScalable:YES];
					[image setSize:&newsize];
					}
				[image setName:login];
				}
			else
				image=[image free];
			}
		NXZoneFree([self zone],login);
		}
	[self setImage:image];
	[self display];
	return image? TRUE : FALSE;
	}


//-----------------------------------------------------------------------
// DRAWING
//-----------------------------------------------------------------------

- drawSelf:(const NXRect *)rects :(int)rectCount
	{
	[super drawSelf:rects:rectCount];
	PSsetgray(NX_LTGRAY);
	NXRectFill(&bounds);
	if(myImage)
		{
		NXRect frects[5];
		float	fgrays[5]={NX_DKGRAY,NX_DKGRAY,NX_DKGRAY,NX_BLACK,NX_BLACK};
		NXPoint imgorigin={1,2};
		NXSize size;
		
		// [TRH] Shrinkwrap rects around image.
		[myImage getSize:&size];
		NXSetRect(&frects[0],3,0,size.width+1,size.height+1);
		NXSetRect(&frects[1],0,1,1,size.height+2);
		NXSetRect(&frects[2],0,size.height+2,size.width+2,1);
		NXSetRect(&frects[3],0,1,size.width+1,1);
		NXSetRect(&frects[4],size.width+1,1,1,size.height+2);
		NXRectFillListWithGrays(frects,fgrays,5);
		[myImage composite:NX_SOVER toPoint:&imgorigin];

		}
	else
		{
		NXPoint imgorigin={0,2};
		[[NXImage findImageNamed:"of"] composite:NX_SOVER toPoint:&imgorigin];
		}
	return self;
	}
	

//-----------------------------------------------------------------------
// THAT'S IT
//-----------------------------------------------------------------------

@end
